const nunjucks=require('nunjucks');
function createEnv(path,opts)  //env为创建的nunjucks的模板， path表示文件目录，当中存贮预读模板 opts为参数对象{}
{
    //各个参数
    var autoescape=opts.autoescape===undefined?true:opts.autoescape;
    var noCache=opts.noCache||false;
    var watch=opts.watch||false;
    var throwOnUndefined=opts.throwOnUndefined||false;


    var env=new nunjucks.Environment(         //new nunjucks.Environment(文件加载器);
        new nunjucks.FileSystemLoader(path,                   //文件加载器的目录设置为path(views/),即render调用的相对路径为views/......
        {
            noCache:noCache,
            watch:watch,
        }),
        {
            autoescape:autoescape,
            throwOnUndefined:throwOnUndefined
        });
    if(opts.filters)  //过滤器,可能有多个
    {
        for(var f in opts.filters)
        {
            env.addFilter(f,opts.filters[f]);
        }
    }
    return env;
}

var env=createEnv('views',{
    watch:true,
    filters:
            {
                hex:function(n)
                {
                    return '0x'+n.toString(16);
                }
            }
});

var s=env.render('hello.html',{name:'TSQ'});
console.log(s);

var s1=env.render('extend.html',
{
    header:'Hello',
    body:'bla bla bla......'
});
console.log(s1);